package com.android.project.games;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Dialog;
import android.content.Context;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.android.project.framework.Game;
import com.android.project.framework.Input.TouchEvent;
import com.android.project.framework.gl.Camera2D;
import com.android.project.framework.gl.SpriteBatcher;
import com.android.project.framework.impl.GLGame;
import com.android.project.framework.impl.GLScreen;
import com.android.project.framework.math.OverlapTester;
import com.android.project.framework.math.Rectangle;
import com.android.project.framework.math.Vector2;
import com.android.project.framework.util.AssociationIO;
import com.android.project.framework.util.AssociationWords;
import com.android.project.framework.util.FacebookActivity;
import com.android.project.framework.util.TweetsActivity;

public class SocialScreen extends GLScreen {
	static final int TWITTER_RUNNING = 0;
	static final int FACEBOOK_RUNNING = 1;
	int state;
	Camera2D guiCam;
	SpriteBatcher batcher;
	Vector2 touchPoint;
	Rectangle facebookBounds; 
	Rectangle twitterBounds;
	List<TouchEvent> touchEvents;
	static TweetsActivity twitter;
	static FacebookActivity facebook;
	static boolean handled = false;
	Dialog dialog;
	Rectangle exitBounds;
	boolean result;
	Rectangle faceBounds;
	Rectangle tweetBounds;
	String score;

	public SocialScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		guiCam = new Camera2D(glGraphics, 320, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		twitter = new TweetsActivity(glGame);
		facebook = new FacebookActivity(glGame);
		touchPoint = new Vector2();
		twitterBounds = new Rectangle(160 - 80, 220 - 18, 160, 36);
		facebookBounds = new Rectangle(160 - 80, 160 - 18,160, 36);
		
		faceBounds = new Rectangle(160 - 24, 160 -  24, 48, 48);
		tweetBounds = new Rectangle(160 - 24, 220 - 24, 48, 48);
		
		exitBounds = new Rectangle(0, 0, 40, 40);
		twitter.checkForSavedLogin();
		
		Settings.load(game.getFileIO());
		
		
	}
	
	


	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		score = String.valueOf(Settings.score);
		int len =  touchEvents.size();
		for(int i = 0; i < len; i++){
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_UP){
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);
				
				if(OverlapTester.pointInRectangle(twitterBounds, touchPoint) && !Settings.twitter){
						Assets.playSound(Assets.clickSound);
						if(twitter.checkForSavedLogin() == null){
							//twitter.getConsumerProvider();
							
							twitter.askOAuth();
							
						}	
						AssociatedWordsActivity.notTwitter = false;
						
						
						return;		
				 }
				
				else if(OverlapTester.pointInRectangle(facebookBounds, touchPoint) && !Settings.facebook){
					Assets.playSound(Assets.clickSound);
					facebook.login();
					
				}
				else if(OverlapTester.pointInRectangle(faceBounds, touchPoint)){
					Message msg = new Message();
					msg.obj = "Post inviato!";
					glGame.getToastHandler().sendMessage(msg);
					facebook.postWall(score);
				
					return;		
				}
				else if(OverlapTester.pointInRectangle(tweetBounds, touchPoint) ){
					Message msg = new Message();
					msg.obj = "Tweet inviato!";
					glGame.getToastHandler().sendMessage(msg);
					twitter.checkForSavedLogin();
					twitter.tweet(score);							
					return;		
				}
				else if(OverlapTester.pointInRectangle(exitBounds, touchPoint)){
					Assets.playSound(Assets.clickSound);
					game.setScreen(new MainMenuScreen(game));
					
					return;		
				}
			}
		}

	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		
		
		
		GL10 gl = glGraphics.getGl();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
       
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.background);
        
        batcher.drawSprite(160, 240, 320, 480, Assets.backgroundRegion);
        batcher.endBatch();
        
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		batcher.beginBatch(Assets.items);
		batcher.drawSprite(160, 400, 294, 44, Assets.social);
		batcher.drawSprite(160, 260, 300, 200, Assets.network);
		if(!Settings.facebook){
			batcher.drawSprite(160, 160, 160, 36, Assets.facebook);
		}
		else {
			batcher.drawSprite(160, 160, 48, 48, Assets.faceIconRegion);
		}	
		batcher.drawSprite(24, 24, 48 ,48, Assets.wrong);
		if(!Settings.twitter){
			batcher.drawSprite(160, 220, 160, 36, Assets.twitter);
		}
		else {
			batcher.drawSprite(160, 220, 48, 48, Assets.tweetIconRegion);
		}
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
	
			/*List<AssociationWords>l = AssociationIO.load(game.getFileIO());
			for (int i = 0; i < l.size(); i++){
				Log.d("file associazioni",  " " + l.get(i).getWord1());
			}*/
		/*if(!TweetsActivity.oAuth) {*/
			
		//}
	
		
		
	}

	

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		Settings.save(game.getFileIO());

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		twitter.resume();
        facebook.getFacebook().extendAccessTokenIfNeeded(glGame.getApplicationContext(), null);

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
